from __future__ import annotations

from dataclasses import dataclass, field
from enum import Enum
from typing import Any, Dict, Optional


class Role(str, Enum):
    """LLM message roles used across the chat domain."""

    SYSTEM = "system"
    USER = "user"
    ASSISTANT = "assistant"


@dataclass
class Message:
    """Chat message within a session."""

    role: Role
    content: str
    metadata: Dict[str, Any] = field(default_factory=dict)
    created_at: Optional[Any] = None
    message_id: Optional[str] = None


@dataclass
class ToolCall:
    """Tool invocation requested by the LLM or client."""

    name: str
    parameters: Dict[str, Any] = field(default_factory=dict)


@dataclass
class Session:
    """Chat session aggregate."""

    id: str
    user_id: Optional[str] = None
    title: Optional[str] = None
    created_at: Optional[Any] = None
    updated_at: Optional[Any] = None


@dataclass
class ChatSummary:
    """Lightweight session summary for UI display."""

    session_id: str
    message_count: int
    last_message_time: Optional[Any]
    has_history: bool

    def to_dict(self) -> Dict[str, Any]:
        return {
            "session_id": self.session_id,
            "message_count": self.message_count,
            "last_message_time": self.last_message_time,
            "has_history": self.has_history,
        }
